<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="util.*"%>
<%@ page import="model.Resources"%>
<%@ page import="control.ResourcesControl"%>
<%@ page import="control.ShetuanControl" %>
<%@ page import="jspsmart.upload.*" %>
<%@ page import="javax.servlet.ServletConfig" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>资源信息录入</title>
	</head>
	<body>
	    <center>正在上传文件，请稍等......</center>

		<!-- 上传部分 -->
		<%
		   request.setCharacterEncoding("UTF-8");
		   
		   //获取系统时间
           java.util.Date date = new java.util.Date(); //获得系统时间.
           String resources_uploadtime = new SimpleDateFormat("yyyy-MM-dd").format(date);//将时间格式转换成符合date要求的格式.
		   
		   //通过username获取userId
		 //  String resources_uploadusername=(session.getAttribute("shetuanName")).toString();//获得username
		   //ShetuanControl uc = new ShetuanControl();
		  // int resources_uploaduserId=uc.getShetuanId(resources_uploadusername);
		   
		   ResourcesControl rc=new ResourcesControl();
		   Resources resources=new Resources();
		   
		  
		   String FileName = null;
	 	   String sPath = "/resources/resources";
	 	   SmartUpload mySmartUpload = new SmartUpload();
		   mySmartUpload.initialize(config, request, response);
		   
		   //mySmartUpload.setMaxFileSize(2048 * 1024);
		   //mySmartUpload.setAllowedFilesList("jpg,gif,png,jpeg,bmp,txt,doc,pdf,exe,zip,rar,xls");
		   
		   try{
		      mySmartUpload.upload();//上传文件
		      String resources_name=mySmartUpload.getRequest().getParameter("resources_name");
		      String resources_type=mySmartUpload.getRequest().getParameter("resources_type");
		      String resources_introduction=mySmartUpload.getRequest().getParameter("resources_introduction");
		    
		      //将isrecommend换成String再换回来
		      
		      String s_resources_isrecommend=mySmartUpload.getRequest().getParameter("resources_isrecommend");
		      int resources_isrecommend=Integer.parseInt(s_resources_isrecommend);
		      
		      int i=0;
		      File myFile = mySmartUpload.getFiles().getFile(i);
	          Date currTime2 = new Date();
			  SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMddhhmmssSS", Locale.US);
			  FileName = new String((formatter2.format(currTime2)).getBytes("utf-8"));
			  String ext = myFile.getFileExt();//获得文件后缀
			  resources_type=ext;
			  FileName = FileName +i+ "." + ext;
			  resources_name=resources_name+"."+ext;
			  System.out.println(sPath);
			  System.out.println(FileName);
			  System.out.println(sPath + "/" + FileName);
			  //将文件另存为，已改名
			  myFile.saveAs(sPath + "/" + FileName,mySmartUpload.SAVE_VIRTUAL);
			 
			  resources.setName(resources_name);
			 resources.setType(resources_type);
		      resources.setPath(FileName);
		      resources.setUploadtime(resources_uploadtime);
		      resources.setIntroduction(resources_introduction);
		     // resources.setUploaduserId(resources_uploaduserId);
		      resources.setIsRecommend(resources_isrecommend);
		      if(rc.insertResources(resources)){
		      %>
		      <script language="javascript">
				if(window.confirm("录入成功，是否继续录入?"))
				window.location.href="resources_add.jsp";
				else
  				window.location.href="resources.jsp";
  			  </script>
		      <%} else {%>
		      <script type="text/javascript">
			     if(window.confirm("录入失败，是否继续录入?"))
			        window.location.href="resources_add.jsp";
			     else
			       window.location.href="resources.jsp";
		     </script>
		     <%
			 }
		   } catch (Exception e) {
			  e.printStackTrace();
			  String errors="<li>文件下载失败：请检查选择的文件是否存在？</li>";
      	      request.setAttribute("errors",errors);
		   }
		     %>
		   
		   
		   
	</body>
</html>